package com.lqm.web.controller.home;

import com.lqm.entity.home.HomeUser;
import com.lqm.service.home.HomeUserService;
import com.lqm.service.home.RechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("home")
public class RechargeController {



    @Autowired
    RechargeService rechargeService;
    @Autowired
    HomeUserService homeUserService;

    @RequestMapping("recharge")
    public Map<String,Object> recharge(Double money,HttpSession session)  {

        HashMap<String, Object> map = new HashMap<>(0);
        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");
        if (homeUser!=null){
            boolean recharge = rechargeService.recharge(money, homeUser);
            HomeUser one = homeUserService.getOneById(homeUser.getId());
            one.setProjects(null);
            one.setUserAddresses(null);
            one.setPassword(null);
            if (recharge){
                session.setAttribute("home_user",one);
                map.put("code",0);
                map.put("msg","充值成功");
            }
        }else {
            map.put("code",101);
            map.put("msg","充值失败");
        }

        return map;
    }

/*    @RequestMapping("get")
    public HomeUser aa(Integer id){
        HomeUser oneById = homeUserService.getOneById(id);
        oneById.setProjects(null);
        oneById.setUserAddresses(null);
        return  oneById;
    }*/
}
